﻿namespace PowerSong.UserInterface
    {
    using System.Windows;
    using System.Windows.Controls;
    using Microsoft.Win32;

    /// <summary>
    /// Interaction logic for SaveFileControl.xaml
    /// </summary>
    public partial class SaveFileControl : UserControl
        {

        public string DialogTitle { get; set; }

        public string DialogFilter { get; set; }

        public string FileName
            {
            get { return FileNameTextBox.Text; }
            set { FileNameTextBox.Text = value; }
            }

        public SaveFileControl()
            {
            this.DialogTitle = "File Selection";
            this.DialogFilter = "All Files|*.*";
            InitializeComponent();
            }

        private void SelectFileButton_Click(object sender, RoutedEventArgs e)
            {

            // Create a dialog box
            var dialog = new SaveFileDialog();

            // Configure the dialog box
            dialog.FileName = FileNameTextBox.Text;
            dialog.AddExtension = true;
            dialog.Title = DialogTitle;
            dialog.Filter = DialogFilter;
            dialog.OverwritePrompt = true;
            dialog.CheckPathExists = true;
            dialog.ValidateNames = true;

            // Show the dialog
            var result = dialog.ShowDialog();
            if (result == true)
                {

                // Update the file name
                FileNameTextBox.Text = dialog.FileName;

                }

            }

        public static string RequestFileName(string filter, string originalFileName = "")
            {

            // Create a dialog box
            var dialog = new SaveFileDialog();

            // Configure the dialog box
            dialog.FileName = originalFileName;
            dialog.AddExtension = true;
            dialog.Title = "Save File";
            dialog.Filter = filter;
            dialog.OverwritePrompt = true;
            dialog.CheckPathExists = true;
            dialog.ValidateNames = true;

            // Show the dialog
            var result = dialog.ShowDialog();
            if (result == true)
                {
                return dialog.FileName;
                }
            else
                {
                return null;
                }

            }

        }

    }
